package com.zhuozhou.jdyc.wight.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;

import com.bumptech.glide.Glide;
import com.zhuozhou.jdyc.R;
import com.zhuozhou.jdyc.databinding.SeeImageDialogBinding;
import com.zhuozhou.jdyc.util.apiUtil.StringUtil;

import java.io.File;
import java.io.FileOutputStream;
import java.util.Calendar;


/**
 * Created by 方舟 on 2018/3/29.
 * describe:选择照片和拍照弹框
 */
public class SeeImageDialog extends Dialog {
    private String url;
    private Bitmap bitmap;
    private Context context;


    public SeeImageDialog(@NonNull Context context, String url) {
        super(context, R.style.ActionSheetDialogStyle);
        this.context = context;
        this.url = url;
    }

    public SeeImageDialog(@NonNull Context context, Bitmap bitmap) {
        super(context, R.style.ActionSheetDialogStyle);
        this.context = context;
        this.bitmap = bitmap;
    }

    public SeeImageDialog builder() {
        initView();
        return this;
    }


    private void initView() {
        SeeImageDialogBinding binding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.see_image_dialog, null, false);
        setContentView(binding.getRoot());//核心代码
        if (StringUtil.isNotEmpty(url)) {
            Glide.with(context).load(url).into(binding.image1);
            binding.tvBaocun.setVisibility(View.GONE);
            binding.image2.setVisibility(View.GONE);
            binding.image1.setVisibility(View.VISIBLE);
        } else if (bitmap != null) {
            Glide.with(context).load(bitmap).into(binding.image2);
            binding.tvBaocun.setVisibility(View.VISIBLE);
            binding.image2.setVisibility(View.VISIBLE);
            binding.image1.setVisibility(View.GONE);
        } else {
            dismiss();
            return;
        }
        binding.tvBaocun.setOnClickListener(l -> {
            saveImage(bitmap);
            dismiss();
        });
        Window dialogWindow = getWindow();
        if (dialogWindow == null) {
            return;
        }
        setCanceledOnTouchOutside(true);
        dialogWindow.setLayout(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT);
        dialogWindow.setGravity(Gravity.CENTER);
    }

    /**
     * 保存图片
     *
     * @param bmp
     */
    public void saveImage(Bitmap bmp) {

        File appDir = new File("/sdcard/Pictures/jdyc/");
        if (!appDir.exists()) {
            appDir.mkdirs();
        }
        String filename = Calendar.getInstance().getTimeInMillis() + "";
        File file = new File("/sdcard/Pictures/jdyc/" + filename + ".jpg");
        if (file.exists()) {
            file.delete();
        }
        if (bmp != null)
            try {
                bmp.describeContents();
                FileOutputStream fos = new FileOutputStream(file.getAbsoluteFile());
                if (fos != null) {
                    bmp.compress(Bitmap.CompressFormat.JPEG, 90, fos);
                    fos.close();
//                    MediaStore.Images.Media.insertImage(getContentResolver(), BitmapFactory.decodeFile(file.getAbsolutePath()), file.getName(), null);
                    Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
                    Uri uri = Uri.fromFile(file);
                    intent.setData(uri);
                    context.sendBroadcast(intent);//java.lang.IllegalStateException: Can't compress a recycled bitmap
                    Toast.makeText(context, "保存成功", Toast.LENGTH_SHORT).show();//open failed: ENOENT (No such file or directory)
                }
            } catch (Exception e) {
                e.printStackTrace();
                file.delete();
                Toast.makeText(context, "保存失败", Toast.LENGTH_SHORT).show();
            } finally {
                //bmp.recycle();
            }
        else {
            file.delete();
            Toast.makeText(context, "保存失败", Toast.LENGTH_SHORT).show();
        }
    }
}
